/*
 * @Author: yanjikun
 * @Date: 2022-06-02 09:48:48
 * @LastEditors: yanjikun
 * @LastEditTime: 2022-06-02 11:11:17
 * @Description: file content
 */

import { uploadFile } from '@/api/global'
import { BasicTinymceProps } from '../basic-tinymce'
import { TinymceEmitType } from '../types/tinymce'
import { TinymceState } from './useTinymceState'

export type TinymceMethods = ReturnType<typeof useTinymceMethods>

export type UseTinymceMethodsContext = {
  state: TinymceState
  props: BasicTinymceProps
  emit: TinymceEmitType
}

export const useTinymceMethods = () => {
  /**自定义图片上传 */
  const imageUploadHandler = async (
    blobInfo: { blob: () => string | Blob },
    success: (arg0: string) => void,
    failure: (arg0: unknown) => void,
  ) => {
    const formData = new FormData()
    formData.append('file', blobInfo.blob())
    try {
      const data = await uploadFile(formData)
      // 上传动作，自定义上传
      const fileUrl = `${location.origin}/${data.filePath}`
      success(fileUrl)
    } catch (error) {
      failure(error)
    }
  }
  return { imageUploadHandler }
}
